package com.snoob.bffAuth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.snoob.baseFeign.service.serviceAdmin.ServiceAdminUserFeign;
import com.snoob.bffAuth.service.SysUserService;
import com.snoob.bffAuth.wConfig.properties.SysProperties;
import com.snoob.bffAuth.wConfig.redis.RedissonSingleService;
import com.snoob.springcloudalibaba.base.BaseException;
import com.snoob.springcloudalibaba.base.BaseLoginUserInfo;
import com.snoob.springcloudalibaba.constant.SysKeyConstant;
import com.snoob.springcloudalibaba.enums.SysKeyEnum;
import com.snoob.springcloudalibaba.enums.SysStateEnum;
import com.snoob.springcloudalibaba.pipleline.dto.UserLoginDtoReq;
import com.snoob.springcloudalibaba.pipleline.dto.UserLoginDtoRes;
import com.snoob.springcloudalibaba.pipleline.qo.SelectSysUserQo;
import com.snoob.springcloudalibaba.pipleline.vo.SysUserVoRes;
import com.snoob.springcloudalibaba.util.jwt.JwtUtil;
import com.snoob.springcloudalibaba.util.RequestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;

/**
 * @描述：系统用户接口实现类
 * @作者：lixing_java@163.com
 * @时间：2022/8/6 21:31
 */
@Service("bffSysUserServiceImpl")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysProperties sysProperties;

    @Resource
    private RedissonSingleService<String> redissonSingleServiceStr;

    @Resource
    private ServiceAdminUserFeign serviceAdminUserFeign;

    /**
     * @描述 系统用户登陆
     */
    @Override
    public UserLoginDtoRes sysUserLogin(UserLoginDtoReq dto, HttpServletRequest request) throws Exception {
        // 登陆方式：1、账号和密码登录方式登陆 2、短信验证码登录
        String loginType = dto.getLoginType();
        if (SysKeyEnum.LOGIN_TYPE_COMMON.getKey().equals(loginType)) {
            // 查询系统用户
            SysUserVoRes sysUserVoRes = serviceAdminUserFeign.selectSysUserByAccountOrPhone(new SelectSysUserQo(dto.getUserAccount()));
            if (null == sysUserVoRes) {
                throw new BaseException("账号或密码不存在！");
            }
            if (SysStateEnum.STATE_NO.getValue() == sysUserVoRes.getActive()) {
                throw new BaseException(SysStateEnum.RESPONSE_STATUS_FALSE_USER_DISABLE.getDescribe());
            }
            // 校验密码
            // 前台通过公钥加密的密码
//            String userPassword = RSAUtil.URLDecodeBase64RSAByPriK(RSAUtil.PRIVATE_KEY, dto.getUserPassword());
//            //if (!Base64Util.encodedStr(userPassword).equals(sysUserEntity.getUserPassword())) {
//            if (userPassword.equals(sysUserVoRes.getUserPassword())) {
//                throw new BaseException("账号或密码不存在！！");
//            }
            //
            // 在redis中记录用户的登录版本号
            String versionKey = MessageFormat.format(SysKeyEnum.LOGIN_USER_VERSION.getKey(), SysKeyConstant.PROJECTNAME, sysUserVoRes.getUserAccount());
            String versionValue = String.valueOf(System.currentTimeMillis());
            redissonSingleServiceStr.setKey(versionKey, versionValue, sysProperties.getLoginTimeout());
            //
            BaseLoginUserInfo baseLoginUserInfo = new BaseLoginUserInfo();
            baseLoginUserInfo.setIp(RequestUtil.getIpAddress(request));
            baseLoginUserInfo.setVersion(versionValue);
            baseLoginUserInfo.setUserAccount(sysUserVoRes.getUserAccount());
            baseLoginUserInfo.setUserPhone(sysUserVoRes.getUserPhone());
            // 根据登录认证信息和版本号生成token
            JSONObject jwt = new JSONObject();
            jwt.put("startExpires", 1000);
            jwt.put("endExpires", 600000);
            jwt.put(SysKeyEnum.LOGIN_USERINFO.getKey(), JSONObject.toJSONString(baseLoginUserInfo));
            String jwtStr = JwtUtil.createJwtForHs256(jwt);
            //
            UserLoginDtoRes userLoginDtoRes = new UserLoginDtoRes();
            userLoginDtoRes.setToken(jwtStr);
            return userLoginDtoRes;
        } else if (SysKeyEnum.LOGIN_TYPE_SMS.getKey().equals(loginType)) {
            // 校验短信验证码
            //checkSmsCode(dto);
            return null;
        } else {
            throw new BaseException("登陆方式非法");
        }
    }
}
